package com.power.services.systemLog;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.systemLog.SystemLogDAO;
import com.power.model.systemLog.TsSystemLog;
import com.power.model.user.TsSysuser;
import com.power.util.DateTimeUtil;
import com.power.util.StringUtil;

@Service("systemLogService")
public class SystemLogServiceImpl
  implements SystemLogService
{

  @Autowired
  private SystemLogDAO systemLogDAO;

  public void create(TsSystemLog transientObject)
  {
    this.systemLogDAO.save(transientObject);
  }

  public TsSystemLog read(Integer id) {
    return ((TsSystemLog)this.systemLogDAO.load(id));
  }

  public void update(TsSystemLog transientObject) {
    this.systemLogDAO.update(transientObject);
  }

  public void delete(TsSystemLog persistentObject) {
    this.systemLogDAO.delete(persistentObject);
  }

  public void writeLoginLog(TsSysuser userInfo) {
    TsSystemLog systemLog = new TsSystemLog();
    systemLog.setDeptId(0);
    systemLog.setDeptName("管理员");
    systemLog.setPerId(userInfo.getId());
    systemLog.setPerName(userInfo.getUsername());
    systemLog.setOperDate(DateTimeUtil.getCurrentDateTime());
    systemLog.setOperContent("登录管理系统");
    systemLog.setLogType("1");
    this.systemLogDAO.save(systemLog);
  }

  public void writeDoMenu(TsSysuser userInfo, String operContent) {
    TsSystemLog systemLog = new TsSystemLog();
    systemLog.setDeptId(0);
    systemLog.setDeptName("管理员");
    systemLog.setPerId(userInfo.getId());
    systemLog.setPerName(userInfo.getUsername());
    systemLog.setOperDate(DateTimeUtil.getCurrentDateTime());
    systemLog.setOperContent(operContent);
    systemLog.setLogType("2");
    this.systemLogDAO.save(systemLog);
  }

  @SuppressWarnings("unchecked")
public List<TsSystemLog> findByAll(String sort, String dir, String operContent, String perName, String operDate1, String operDate2) {
    DetachedCriteria criteria = DetachedCriteria.forClass(TsSystemLog.class);
    if (operContent.length() > 0) criteria.add(Restrictions.like("operContent", "%" + operContent + "%"));
    if (perName.length() > 0) criteria.add(Restrictions.like("perName", "%" + perName + "%"));
    if ((operDate1.length() > 0) && (operDate1.length() > 0)) {
      criteria.add(Restrictions.between("operDate", DateTimeUtil.switchStringToDate3(operDate1 + " 00:00:00"), DateTimeUtil.switchStringToDate3(operDate2 + " 24:00:00")));
    }
    if (StringUtil.compareString(dir, "ASC"))
      criteria.addOrder(Order.asc(sort));
    else if (StringUtil.compareString(dir, "DESC"))
      criteria.addOrder(Order.desc(sort));
    else {
      criteria.addOrder(Order.asc("logId"));
    }
    return this.systemLogDAO.findByCriteria(criteria);
  }

  @SuppressWarnings("unchecked")
public List<TsSystemLog> findByLogType(String logType, String sort, String dir, String operContent, String perName, String operDate1, String operDate2) {
    DetachedCriteria criteria = DetachedCriteria.forClass(TsSystemLog.class);
    criteria.add(Restrictions.eq("logType", logType));
    if (operContent.length() > 0) criteria.add(Restrictions.like("operContent", "%" + operContent + "%"));
    if (perName.length() > 0) criteria.add(Restrictions.like("perName", "%" + perName + "%"));
    if ((operDate1.length() > 0) && (operDate1.length() > 0))
      criteria.add(Restrictions.between("operDate", DateTimeUtil.switchStringToDate1(operDate1), DateTimeUtil.switchStringToDate1(operDate2)));
    if (StringUtil.compareString(dir, "ASC"))
      criteria.addOrder(Order.asc(sort));
    else if (StringUtil.compareString(dir, "DESC"))
      criteria.addOrder(Order.desc(sort));
    else {
      criteria.addOrder(Order.asc("logId"));
    }
    return this.systemLogDAO.findByCriteria(criteria);
  }

  public int deleteAllKeepSomeDay(int days) {
    return this.systemLogDAO.bulkUpdate("delete  com.power.model.systemLog.TsSystemLog log where log.operDate<sysdate-" + days);
  }
}